<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Guestbook extends CI_Controller {

	
	private $_data = array();
	public $type=array(
		
	);
	public function __construct(){
		parent::__construct();
		$this->load->helper('url');
		
		$this->load->model('guestbook_model');	
		$this->load->library('session');	
	}
	/**
	留言
	*/
	public function index($curpage=0){

		$a = intval(rand(0,20));
		$b = intval(rand(0,20));
		$this->session->set_userdata('yzm',   $a + $b );
		$yzm = $a .'+' . $b . '=';
		$this->_data['yzm']  = $yzm;

		$total_d = $this->guestbook_model->get_lists();
		
		$this->_data['title'] = '留言';

		//分页
		$perpage =5;
		$this->load->library('pagination');
		$cf['base_url'] = site_url('guestbook/index');
		$cf['total_rows'] = count($total_d);
		$cf['per_page'] = $perpage;
		//$cf['uri_segment'] = 2;
		$this->pagination->initialize($cf);
		$this->_data['pagination'] = $this->pagination->create_links();

		$limit = $curpage . ', ' . $perpage;
		$d = $this->guestbook_model->get_lists("*",null,$limit, 'id desc');
		$this->_data['data'] = $d;

		
		$this->load->view('guestbook', $this->_data);
		
	}
	//添加
	public function add(){
		if($this->session->userdata('yzm') != $this->input->post('yzm')){
			redirect('guestbook');		
		}
		$karr = array('name', 'phone','email','title','content');
		$d = $this->input->post();
		$setarr = array();
		$setarr['createtime'] = $setarr['updatetime']  = date("Y-m-d H:i:s");
		foreach($d as $k=>$v){
			if(in_array($k, $karr)){
				$setarr[$k] = htmlspecialchars( $v );
			}
		}
		$this->db->insert('guestbook',$setarr);
		redirect('guestbook');		
	}

	//验证码
	public function chkyzm(){
		$yzm = trim($this->input->post('yzm'));
		if($yzm && $yzm == $this->session->userdata('yzm')){
			echo "1"; //验证码正确
		}else{
			echo "2";
		}
	}
	
	
}

/* End of file guestbook.php */
/* Location: ./application/controllers/guestbook.php */